package tp.pr3.commands;

import java.util.StringTokenizer;
import tp.pr3.Directions;
import tp.pr3.Door;
import tp.pr3.Game;
import tp.pr3.Player;
import tp.pr3.Room;
import tp.pr3.commands.exceptions.CommandExecutionException;
import tp.pr3.commands.exceptions.WrongCommandFormatException;

public class GoCommand implements Command
{

	private Directions _direccion;
	private Game _game;
	private Player _friki;
	private Door _puerta;
	private Room _cuarto;

	public GoCommand()
	{
		this._direccion = null;
		this._game=null;
		this._friki=null;
		this._puerta=null;
		this._cuarto=null;
	}

	@Override public void execute() throws CommandExecutionException
	{

		if (this._direccion.equals(Directions.UNKNOWN))
			throw new CommandExecutionException();
		this._cuarto = this._game.getCurrentRoom();
		this._puerta = this._game.getCurrentMap().getDoor(this._cuarto,
				this._direccion);
		if (this._puerta == null)
			throw new CommandExecutionException(
					"What the hell am I supposed to do going to "
							+ this._direccion.toString() + " ?");

		if (!this._puerta.isOpen())

			throw new CommandExecutionException("There is a door in the "
					+ this._direccion.toString() + ", but it is closed.");

		Room nextRoom = this._puerta.nextRoom(this._cuarto);
		if (!this._puerta.connect(this._cuarto, nextRoom))
			throw new CommandExecutionException(
					"Impossible to go through the door from this side.");

		System.out.println("...mooving to " + this._direccion.toString());
		this._friki.addHealth(-5);
		this._game.setCurrentRoom(nextRoom);
		this._game.getCurrentMap().setCurrentRoom(nextRoom);
		if (this._game.getCurrentMap().getCurrentRoom().isExit())
		{
			this.gameOverMessage();

			this._game.requestQuit();
		}
	}

	@Override public String getHelp()
	{
		return "GO | IR {NORTH|EAST|SOUTH|WEST}\n";
	}

	@Override public Command parse(String cad, Game execContext)
			throws WrongCommandFormatException
	{
		StringTokenizer tkn = new StringTokenizer(cad);

		if (tkn.countTokens() != 2)
			throw new WrongCommandFormatException();
		String verb = tkn.nextToken();
		String where = tkn.nextToken();
		if (!(verb.equalsIgnoreCase("GO") || verb.equalsIgnoreCase("IR")))
			throw new WrongCommandFormatException();
		try
		{

			this._direccion = Directions.valueOf(where.toUpperCase());
			this._game = execContext;
			this._friki = this._game.getPlayer();
		} catch (Exception e)
		{
			throw new WrongCommandFormatException(e);
		}

		return this;
	}

	public Directions getDirection()
	{
		return this._direccion;
	}

	private void gameOverMessage()
	{

		System.out.println("GAME OVER");
		System.out.println(" Thank you for playing, goodbye.");
		String salud, score;
		salud = Integer.toString(this._game.getPlayer().getHealth());
		score = Integer.toString(this._game.getPlayer().getPoints());
		System.out.println("HEALTH = " + salud + ", SCORE =" + score);

	}
}
